package blf.librarysystem.Controller;

import blf.librarysystem.Pojo.Admin;
import blf.librarysystem.Service.AdminService;
import blf.librarysystem.Utils.MD5Util;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private AdminService adminService;

    @GetMapping("/login")
    public String sys_index(){
        return "admin/login";
    }

    @GetMapping("/sys_index")
    public String index(){
        return "admin/index";
    }

    @PostMapping("/login")
    public String userlogin(@RequestParam("adminname") String name, @RequestParam("password") String pwd, HttpSession session) {

        String MD5_pwd = MD5Util.MD5Encode(pwd, "UTF-8");
        Admin admin = adminService.login(name, MD5_pwd);
        if (admin != null) {
            session.setAttribute("name", admin.getAdminName());
            session.setAttribute("pwd", admin.getAdminPassword());
            return "redirect:/admin/sys_index";//浏览器地址栏路由发生变化，转去sys_index.html进行显示
        } else {
            session.setAttribute("errorMsg", "用户名或密码错误，请重新登录!");
            return "admin/login";
        }
    }

    @GetMapping("/welcome")
    public String welcome(){
        return "admin/welcome";
    }

}
